﻿/*San Antonio Health Center Patient Information Management System
 * 
 * ===================================
 * Description: addFamily Page
 * Updates:
 * -adds new family in the database
 * -connects and stores in database
 * 
 * Note: 
 * -check for valid date not yet done
 * -----------------------------------
 * Author:
 * Guerrero,Ida Louise M.
 * 
 * Date: Feb. 22, 2010
 * ====================================
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Text.RegularExpressions;

namespace sahcpims
{
    public partial class addFamily : Form
    {
        public addFamily()
        {
            InitializeComponent();
        }

        public string homeEmpName;
        public int homeEmpNum;
        public int doFlag = 0;

        public void putAccountInfo()
        {
            empNumLabel.Text = homeEmpNum.ToString();
            employeeLabel.Text = homeEmpName;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void submit_Click(object sender, EventArgs e)
        {
            /*  DateTime bday = new DateTime();
                string bbirth;
                bday.Date.Equals("2/22/2009");
                bbirth = DateTime.Today.Subtract(bday).Days.ToString();
                MessageBox.Show(bbirth);x
                MessageBox.Show(DateTime.Today.ToShortDateString());
             */

            //Storage of Form Values
            string fname = Firstname.Text;
            string mname = Middlename.Text;
            string lname = Lastname.Text;
            string bdate = mon.Text + "/" + day.Text + "/" + year.Text;
            string address = Address.Text;

            //Match Values to Valid inputs
            Match fnameMatch = System.Text.RegularExpressions.Regex.Match(fname, @"^[A-Z][a-zA-Z]*$");
            Match mnameMatch = System.Text.RegularExpressions.Regex.Match(mname, @"^[A-Z][a-zA-Z]*$");
            Match lnameMatch = System.Text.RegularExpressions.Regex.Match(lname, @"^[A-Z][a-zA-Z]*$");
            Match addMatch = System.Text.RegularExpressions.Regex.Match(address, @"[0-9a-zA-Z]+$");


            if (!fnameMatch.Success) MessageBox.Show("Invalid Firstname Input");
            else if (!mnameMatch.Success) MessageBox.Show("Invalid Middlename Input");
            else if (!lnameMatch.Success) MessageBox.Show("Invalid Lastname Input");
            else if (mon.Text.Equals("") || day.Text.Equals("")) MessageBox.Show("Invalid Birthdate Input");
            else if (year.Text.Equals("")) MessageBox.Show("Invalid Birthdate Input");
            else if (!addMatch.Success) MessageBox.Show("Invalid Address Input");
            else
            {

                string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\SAHCPIMS_database.mdb";

                OleDbConnection conn = new OleDbConnection(conString);

                DataSet ds = new DataSet();
                try
                {

                    conn.Open();
                    OleDbDataAdapter adapter = new OleDbDataAdapter("INSERT into Family (FirstName, MiddleName, LastName, Birthday, Address) values('" + fname + "', '" + mname + "', '" + lname + "', '" + bdate + "', '" + address + "')", conn);

                    adapter.Fill(ds);

                    doFlag = 0;
                }
                catch (OleDbException)
                {

                    memberVerificationLabel.Text = "Message: Data not saved in Database. There is a problem with the input.";
                    doFlag = 1;
                }

                catch (FormatException)
                {

                    memberVerificationLabel.Text = "Message: Invalid Input. Try Again.";
                    doFlag = 1;

                }
                conn.Close();

                if (doFlag == 0)
                {

                    addFamilyLabel.Text = "Add Family Success!";
                    addfamilyPanel.Visible = false;
                    Submit.Visible = false;
                    Back.Visible = true;
                    label8.Visible = false;
                    label9.Visible = false;
                    label10.Visible = false;
                    label4.Visible = false;
                    label5.Visible = false;
                    cancel.Visible = false;
                }
                /*DataTable dt = ds.Tables[0];
                foreach (DataRow dr in dt.Rows)
                    */
            }
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            Firstname.ResetText();
            Middlename.ResetText();
            Lastname.ResetText();
            mon.ResetText();
            day.ResetText();
            year.ResetText();
            Address.ResetText();
        }

        private void mon_SelectedIndexChanged(object sender, EventArgs e)
        {

            // if (mon.Text == "02")
            //  day.Select(1, 28);


        }

        private void logOutButton_Click(object sender, EventArgs e)
        {
            logIn logout = new logIn();
            logout.Visible = true;
            this.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            search addFamilyForm = new search();
            addFamilyForm.employeeName = homeEmpName;
            addFamilyForm.employeeNumber = homeEmpNum;
            addFamilyForm.putAccountInfo();
            addFamilyForm.addMemberFlag = 1;
            addFamilyForm.Visible = true;
            this.Visible = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            search addFamilyForm = new search();
            addFamilyForm.employeeName = homeEmpName;
            addFamilyForm.employeeNumber = homeEmpNum;
            addFamilyForm.putAccountInfo();
            addFamilyForm.Visible = true;
            this.Visible = false;
        }

        private void Back_Click(object sender, EventArgs e)
        {
            homePage home = new homePage();
            home.homeEmpName = homeEmpName;
            home.homeEmpNum = homeEmpNum;
            home.Visible = true;
            this.Visible = false;
        }
    }
}